package day1212zy.day1209;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class demo4 {
    public static void main(String[] args) throws DocumentException {
        ArrayList<Student> list = new ArrayList<>();
//        获取sax解析器
        SAXReader reader = new SAXReader();
//        通过sax解析器读取xml文件，返回dom对象
        Document dom = reader.read(new File("src\\day1212zy.day1209\\student.xml"));
//        通过dom对象获取根元素
        Element root = dom.getRootElement();
//        展示所有的子元素的文本内容
        List<Element> slist = root.elements("student");
        for (Element e : slist) {
            String s1 = e.attributeValue("stuNo");
            String s2 = e.elementText("name");
            String s3 = e.elementText("age");
            String s4 = e.elementText("sex");
            String s5 = e.elementText("score");
            list.add(new Student(s1,s2,Integer.parseInt(s3),s4,Double.parseDouble(s5)));
        }
        for (Student r : list) {
            System.out.println(r);
        }
    }
}
